package com.example.certificate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.certificate.dto.InsDataTypeDto;
import com.example.certificate.dto.UpdateStatusDTO;
import com.example.certificate.dto.common.BasePageDataPageDto;
import com.example.certificate.dto.common.DelByIdDto;
import com.example.certificate.entity.DataType;
import com.example.certificate.entity.DataTypeBring;
import com.example.certificate.entity.User;
import com.example.certificate.mapper.DataTypeBringMapper;
import com.example.certificate.mapper.DataTypeMapper;
import com.example.certificate.service.DataTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.certificate.utils.ResultConstant;
import com.example.certificate.utils.ResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hao
 * @since 2022-03-08 11:01:39
 */
@Service
public class DataTypeServiceImpl extends ServiceImpl<DataTypeMapper, DataType> implements DataTypeService {

    @Resource
    private DataTypeMapper dataTypeMapper;


    @Override
    public ResultEntity insertDataType(InsDataTypeDto dto) {
        DataType dataType = new DataType();
        dataType.setStatus(1);
        dataType.setType(dto.getType());
        dataType.setImage(dto.getImage());
        dataType.setDescDetail(dto.getDescDetail());
        int insert = dataTypeMapper.insert(dataType);
        if(insert > 0){
            return new ResultEntity();
        }
        return new ResultEntity(ResultConstant.INTERNAL_SERVER_ERROR);
    }

    @Override
    public ResultEntity delDataTypeById(DelByIdDto dto) {
        dataTypeMapper.deleteById(dto.getId());
        return new ResultEntity();
    }

    @Override
    public ResultEntity updateStatus(UpdateStatusDTO dto) {
        dataTypeMapper.updateStatus(dto.getId(),dto.getStatus());
        return new ResultEntity();
    }

    @Override
    public ResultEntity selDataTypePage(BasePageDataPageDto dto) {
        Page<DataType> page = new Page<>();
        page.setCurrent(dto.getCurrent() == null ? 1 : dto.getCurrent());
        page.setPages(dto.getPageSize() == null ? 10 : dto.getPageSize());
        return new ResultEntity(dataTypeMapper.selPage(page,dto));
    }

    @Override
    public ResultEntity selDataTypeList() {
        Map<String, List<DataType>> hashMap = new HashMap<>();
        List<DataType> type1 = dataTypeMapper.selectList(new QueryWrapper<DataType>()
                .eq("status", 1)
                .eq("type",1));

        List<DataType> type2 = dataTypeMapper.selectList(new QueryWrapper<DataType>()
                .eq("status", 1)
                .eq("type",2));

        hashMap.put("type1",type1);
        hashMap.put("type2",type2);

        return new ResultEntity(hashMap);
    }


}
